package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.my.contract.ShowSettingInfoContract;
import com.fernfx.xingtan.my.presenter.ShowSettingInfoPresenter;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.Base64Util;
import com.fernfx.xingtan.utils.SPUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/14.
 * 显示设置资料信息
 */

public class ShowSettingInfoActivity extends BaseActivity implements ShowSettingInfoContract.View, View.OnClickListener {
    @BindView(R.id.tv_refund_project_title)
    TextView projectTitle;
    @BindView(R.id.show_setting_info_clt)
    ViewGroup showSettingInfoClt;
    @BindView(R.id.nickname_eidt)
    EditText nickNameEdit;
    @BindView(R.id.man_status_logo_tv)
    TextView manStatusLogoTv;
    @BindView(R.id.woman_status_logo_tv)
    TextView womanStatusLogoTv;
    @BindView(R.id.man_rlt)
    View manRlt;
    @BindView(R.id.woman_rlt)
    View womanRlt;
    @BindView(R.id.qrcode_iv)
    ImageView qrCodeIv;
    @BindView(R.id.autograph_eidt)
    EditText autographEidt;

    //presenter
    private ShowSettingInfoContract.Presenter presenter;

    //标题名key
    public static final String TITLE_TEXT = "title_text_key";
    //功能区分
    public static final String FUNCTION_DIVISION = "function_division_key";
    //旧的值key
    public static final String OLD_TEXT = "old_text";

    //动态显示集合
    private static final Map<Integer, Integer> VIEWS_MAP = initViewMap();
    //处理逻辑的handler
    private Handler fDivisionHandler;
    //性别状态
    private boolean sex_flag = true;
    //判断是否点击过
    private int clickCount = 0;
    //二维码保存的key
    public String qrcodeKey = "my_qr_code_sp";

    public static void start(BaseActivity baseActivity, String titleText, int result, String oldText) {
        Intent intent = new Intent(baseActivity, ShowSettingInfoActivity.class);
        //动态设置名称
        intent.putExtra(TITLE_TEXT, titleText);
        intent.putExtra(FUNCTION_DIVISION, result);
        intent.putExtra(OLD_TEXT, oldText);
        baseActivity.startActivityForResult(intent, result);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_show_setting_info;
    }

    @Override
    protected void initView() {
        //init presenter
        presenter = new ShowSettingInfoPresenter();

        Intent intent = getIntent();
        if (intent.hasExtra(TITLE_TEXT)) {
            setToolbarTitle(intent.getStringExtra(TITLE_TEXT));
        }
        //设置点击按钮属性
        projectTitle.setText("完成");
        projectTitle.setTextColor(getResources().getColor(R.color.green));
        projectTitle.setVisibility(View.VISIBLE);
        //动态显示
        int fDivisionText = intent.getIntExtra(FUNCTION_DIVISION, 0);
        if (VIEWS_MAP.containsKey(fDivisionText)) {
            //获取之前的内容并做相应的处理
            String oldText = intent.getStringExtra(OLD_TEXT);

            presenter.displayAppointView(showSettingInfoClt, VIEWS_MAP.get(fDivisionText));
            //发送消息的message
            Message message = fDivisionHandler.obtainMessage();
            message.what = fDivisionText;
            message.obj = oldText;
            fDivisionHandler.sendMessage(message);
        }
    }

    @Override
    protected void initData() {
        presenter.init(this);
        //设置唯一的二维码
        qrcodeKey += AccountManager.getUserInfo().getObj().getMobile();
    }

    @OnClick({R.id.woman_rlt, R.id.man_rlt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.woman_rlt:
                clickCount += 1;
                //设置显示
                sex_flag = false;
                manStatusLogoTv.setVisibility(View.GONE);
                womanStatusLogoTv.setVisibility(View.VISIBLE);
                break;
            case R.id.man_rlt:
                clickCount += 1;
                //设置显示
                sex_flag = true;
                manStatusLogoTv.setVisibility(View.VISIBLE);
                womanStatusLogoTv.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void onBeforeSetContentLayout() {
        fDivisionHandler = new Handler(getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                //设置之前旧的值
                final String oldText = msg.obj.toString();

                switch (msg.what) {
                    case MySettingActivity.NICK_NAME_RESULT:
                        nickNameEdit.setText(oldText);
                        //弹出输入软键盘
                        SoftInputUtil.showSoftInputFromWindow(mContext, nickNameEdit);
                        projectTitle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String nickNameText = trimEditText(nickNameEdit);
                                if (TextUtils.isEmpty(nickNameText)) {
                                    ToastUtil.showCentertoast("昵称不能为空");
                                    return;
                                }
                                //验证两次昵称是否一致
                                if (nickNameText.equals(oldText)) {
                                    ToastUtil.showCentertoast("昵称没有修改");
                                    return;
                                }
                                requestArgsMap.clear();
                                requestArgsMap.put("nickname", nickNameText);
                                presenter.updateNickName(requestArgsMap, nickNameText);
                            }
                        });
                        break;
                    case MySettingActivity.SEX_RESULT:
                        //更改性别
                        if (Constant.ApplicationVariable.SEX_STATUS.equals(oldText)) {
                            womanStatusLogoTv.setVisibility(View.VISIBLE);
                            manStatusLogoTv.setVisibility(View.GONE);
                        } else {
                            manStatusLogoTv.setVisibility(View.VISIBLE);
                            womanStatusLogoTv.setVisibility(View.GONE);
                        }
                        projectTitle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (clickCount == 0) {
                                    ToastUtil.showCentertoast("请重新选择,内容与之前一致");
                                    return;
                                }
                                requestArgsMap.clear();
                                String sexText = sex_flag ? "1" : "2";
                                requestArgsMap.put("sex", sexText);
                                presenter.updateSex(requestArgsMap, sexText);
                            }
                        });
                        break;
                    case MySettingActivity.CALLING_CARD_RESULT:
                        //获取名片二维码
                        String base64Text = SPUtil.readString(qrcodeKey);
                        if (!TextUtils.isEmpty(base64Text)){
                            showWoQrcode(base64Text);
                        }else {
                            requestArgsMap.clear();
                            presenter.bussinessCard(requestArgsMap);
                        }
                        break;
                    case MySettingActivity.AUTOGRAPH_RESULT:
                        autographEidt.setText(oldText);
                        projectTitle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String autoGraphText = trimEditText(autographEidt);
                                if (TextUtils.isEmpty(autoGraphText)) {
                                    ToastUtil.showCentertoast("签名内容不能为空");
                                    return;
                                }
                                requestArgsMap.clear();
                                requestArgsMap.put("signature",autoGraphText);
                                presenter.updateAutograph(requestArgsMap,autoGraphText);
                            }
                        });
                        break;
                }
            }
        };
    }

    public String getQrcodeKey(){
        return qrcodeKey;
    }

    @Override
    public void showWoQrcode(String base64Text) {
        if (TextUtils.isEmpty(base64Text)) {
            return;
        }
        Bitmap bitMap = Base64Util.base64ToBitmap(base64Text);
        qrCodeIv.setImageBitmap(bitMap);
    }

    private static final Map<Integer, Integer> initViewMap() {
        final Map<Integer, Integer> map = new HashMap<>(10);
        //昵称
        map.put(MySettingActivity.NICK_NAME_RESULT, R.id.nickname_lat);
        //性别
        map.put(MySettingActivity.SEX_RESULT, R.id.sex_lat);
        //我的二维码
        map.put(MySettingActivity.CALLING_CARD_RESULT, R.id.qrcode_rlt);
        //签名
        map.put(MySettingActivity.AUTOGRAPH_RESULT, R.id.autograph_lat);
        return map;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
